import express from 'express';
import cors from 'cors';
import path from 'path';
import fs from 'fs';
import videoRoutes from './routes/video.routes';

const app = express();
const port = process.env.PORT || 3000;

// 创建必要的目录
const uploadsDir = path.join(__dirname, '../uploads');
const outputDir = path.join(__dirname, '../output');

if (!fs.existsSync(uploadsDir)) {
  fs.mkdirSync(uploadsDir, { recursive: true });
}
if (!fs.existsSync(outputDir)) {
  fs.mkdirSync(outputDir, { recursive: true });
}

// 中间件
app.use(cors());
app.use(express.json());
app.use(express.urlencoded({ extended: true }));

// 静态文件服务
app.use('/uploads', express.static(uploadsDir));
app.use('/output', express.static(outputDir));

// 路由
app.use('/api', videoRoutes);

// 错误处理
app.use((err: any, req: express.Request, res: express.Response, next: express.NextFunction) => {
  console.error('错误:', err);
  res.status(500).json({
    success: false,
    message: err.message || '服务器内部错误'
  });
});

// 启动服务器
app.listen(port, () => {
  console.log(`服务器运行在 http://localhost:${port}`);
  console.log('上传目录:', uploadsDir);
  console.log('输出目录:', outputDir);
}); 